/// <reference path="../Scripts/jquery-1.7.1.min.js" />
/// <reference path="../Scripts/knockout-2.1.0.js" />

function LogViewModel() 
	{
    var self = this;
	// put a property loop here
	self.LogID = ko.observable();
			self.Priority = ko.observable();
			self.Severity = ko.observable();
			self.Title = ko.observable();
			self.MachineName = ko.observable();
			self.AppDomainName = ko.observable();
			self.Message = ko.observable();
			self.TS = ko.observable();
			self.CreatedBy = ko.observable();
			self.CreatedDt = ko.observable();
			self.ModifiedBy = ko.observable();
			self.ModifiedDt = ko.observable();
			self.ApplicationID = ko.observable();
			self.LogCategoryID = ko.observable();
			self.Action = ko.observable();
			self.Caller = ko.observable();
			self.Role = ko.observable();
			self.PageSize = ko.observable();
			self.PageNumber = ko.observable();
			self.SortColumn = ko.observable();
			self.SortType = ko.observable();
			self.TotalRows = ko.observable();
    
    self.LogList = ko.observableArray([]);
    self.setLog = function (
			LogID, Priority, Severity, Title, MachineName, AppDomainName, Message, TS, CreatedBy, CreatedDt, ModifiedBy, ModifiedDt, ApplicationID, LogCategoryID, _Action, _Caller, _Role, _PageNumber, _PageSize, _SortColumn, _SortType, _TotalRows 
	
	) {

	LogID = _LogID;
			Priority = _Priority;
			Severity = _Severity;
			Title = _Title;
			MachineName = _MachineName;
			AppDomainName = _AppDomainName;
			Message = _Message;
			TS = _TS;
			CreatedBy = _CreatedBy;
			CreatedDt = _CreatedDt;
			ModifiedBy = _ModifiedBy;
			ModifiedDt = _ModifiedDt;
			ApplicationID = _ApplicationID;
			LogCategoryID = _LogCategoryID;
			Action = _Action;
			Caller = _Caller;
			Role = _Role;
			PageSize = _PageSize
			PageNumber = _PageNumber;
			SortColumn = _SortColumn;
			SortType = _SortType;
			TotalRows = _TotalRows;
        
    }; //  End of set procedure 



    //Add Application
	self.AddLog = function () {
	    var self = this;
	    self.Action = "Add";
	    self.Caller = "trooks";
	    self.Role = "user";

	    $(".form").validate({ rules: { txtApplicationName: { required: true } } });
	    if (!$(".form").valid()) {
	        //alert("Invalied");
	        return;
	    }
	    jQuery.ajax({
	        url: "./api/Logapi/",
	        data: ko.toJSON({ ApplicationName: self.ApplicationName, Action: self.Action, Caller: self.Caller }),
	        type: 'POST',
	        contentType: 'application/json; charset=utf-8',
	        success: function (result) {
	            //alert(result);
	            $('#myModaladd').modal('hide');
	            self.GetApplicationByParameter();
	        },
	        error: function (xhr, ajaxOptions, thrownError) {
	            $('#error-dis').text(thrownError);
	            $('#register-error').css({ 'display': 'block' });
	            $('#SearchFilter-home').css({ 'display': 'none' });
	            $('#myModaladd').modal('hide');
	            $(document).scrollTop(0);
	        }
	    });
	    $(".form")[0].reset();
	};

	self.LoadApplication = function (appVal) {
	    self.LoadingApplication(appVal);
	};

    //Delete Application
	self.DeleteApplicationAction = function (appval) {
	    self.Action = "Remove";
	    self.Caller = "trooks";

	    jQuery.ajax({
	        url: "./api/ApplicationApi/",
	        data: ko.toJSON({ Action: self.Action, Caller: self.Caller, ApplicationID: appval.ApplicationID, TS: appval.TS }),
	        type: 'POST',
	        contentType: 'application/json; charset=utf-8',
	        success: function (result) {
	            $('#myModal').modal('hide');
	            self.GetApplicationByParameter();
	        },
	        error: function (xhr, ajaxOptions, thrownError) {
	            $('#error-dis').text(thrownError);
	            $('#register-error').css({ 'display': 'block' });
	            $('#SearchFilter-home').css({ 'display': 'none' });
	            $('#myModal').modal('hide');
	            $(document).scrollTop(0);
	        }
	    });
	};
    //Edit Application

	self.EditApplication = function (appVal) {
	    self.EditingApplication(appVal);
	};

	self.UpdateApplication = function (appval) {
	    self.Action = "Update";
	    self.Caller = "trooks";
	    self.Role = "user";

	    $(".form1").validate({ rules: { txtApplicationName: { required: true } } });
	    if (!$(".form1").valid()) {
	        return;
	    }
	    jQuery.ajax({
	        url: "./api/ApplicationAPI/",
	        data: ko.toJSON({ ApplicationName: appval.ApplicationName, Action: self.Action, Caller: self.Caller, ApplicationID: appval.ApplicationID, TS: appval.TS, CreatedBy: appval.CreatedBy, CreatedDt: appval.CreatedDt, ModifiedBy: appval.ModifiedBy, ModifiedDt: appval.ModifiedDt }),
	        type: 'POST',
	        contentType: 'application/json; charset=utf-8',
	        success: function (result) {
	            $('#myModalEdit').modal('hide');
	            self.GetApplicationByParameter();
	        },
	        error: function (xhr, ajaxOptions, thrownError) {
	            $('#error-dis').text(thrownError);
	            $('#register-error').css({ 'display': 'block' });
	            $('#SearchFilter-home').css({ 'display': 'none' });
	            $('#myModalEdit').modal('hide');
	            $(document).scrollTop(0);
	        }

	    });



	};


	ko.bindingHandlers.sort = {
	    init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
	        var asc = false;
	        element.style.cursor = 'pointer';

	        element.onclick = function () {
	            var value = valueAccessor();
	            var prop = value.prop;
	            var data = value.arr;

	            asc = !asc;
	            if (asc) {
	                self.ApplicationList.sort(function (left, right) {
	                    return left[prop] == right[prop] ? 0 : left[prop] < right[prop] ? -1 : 1;
	                });
	            } else {
	                self.ApplicationList.sort(function (left, right) {
	                    return left[prop] == right[prop] ? 0 : left[prop] > right[prop] ? -1 : 1;
	                });
	            }
	        }
	    }
	};

    //loading model popup
	ko.bindingHandlers['modal'] = {
	    init: function (element, valueAccessor, allBindingsAccessor) {
	        var allBindings = allBindingsAccessor();
	        var $element = $(element);
	        $element.addClass('hide modal');

	        if (allBindings.modalOptions) {
	            if (allBindings.modalOptions.beforeClose) {
	                $element.on('hide', function () {
	                    return allBindings.modalOptions.beforeClose();
	                });
	            }
	        }

	        return ko.bindingHandlers['with'].init.apply(this, arguments);
	    },
	    update: function (element, valueAccessor) {
	        var value = ko.utils.unwrapObservable(valueAccessor());

	        var returnValue = ko.bindingHandlers['with'].update.apply(this, arguments);

	        if (value) {
	            $(element).modal('show');
	        } else {
	            $(element).modal('hide');
	        }

	        return returnValue;
	    }
	};

    //filter

	self.LogList = ko.observableArray([]);
	self.filter = ko.observable("");

	self.filteredLog = ko.computed(function () {
	    var filterText = self.filter().toLowerCase();
	    return ko.utils.arrayFilter(self.LogList(), function (item) {
	        return filterText.length < 1 || item.LogID == filterText || item.MachineName == filterText || item.Priority == filterText || item.Title.toLowerCase().indexOf(filterText) != -1
	    });

	}, self);



	self.GetLoggingByParameter = function () {
	    self.Action = "GetAll";
	    self.Caller = "trooks";
	    self.Role = "user";
	    self.PageNumber = 1;
	    self.PageSize = 10;
	    self.SortColumn = "LogID";
	    self.SortType = "asc";
	    jQuery.ajax({
	        url: "./api/Logapi/",
	        data: { Action: self.Action, Caller: self.Caller, Role: self.Role, PageNumber: self.PageNumber, PageSize: self.PageSize, SortColumn: self.SortColumn, SortType: self.SortType },
	        type: 'GET',
	        success: function (result) {
	            self.LogList.removeAll();
	            $.each(result, function (key, val) {
	                self.LogList.push(
                        val
                    )
	            })
	        },
	        error: function (xhr, ajaxOptions, thrownError) {
	            $('#error-dis').text(thrownError);
	            $('#register-error').css({ 'display': 'block' });
	            $('#SearchFilter-home').css({ 'display': 'none' });
	            $(document).scrollTop(0);
	        }
	    });

	    if (loginUserRole.indexOf('super admin') <= -1) {
	        $('.add-btn').attr('disabled', 'disabled');
	    }
	};


	

}; //End of view model





